#pragma once

#ifndef _BBOX_H_
#define _BBOX_H_

#include <maya/MFloatPoint.h>
#include "tribox.h"

struct Idx3
  {
    int x;
    int y;
    int z;
	Idx3(int x1,int y1, int z1){x=x1; y=y1; z=z1;};
	Idx3(){};
  };

enum NEIGHBORING {vertex,edge,face};

class BBox
{
public:

  void center(float& ctr_x, float& ctr_y, float& ctr_z) const
  {
    ctr_x = (xmax + xmin) / 2.0f;
    ctr_y = (ymax + ymin) / 2.0f;
    ctr_z = (zmax + zmin) / 2.0f;
  }
  void extents(float& ext_x, float& ext_y, float& ext_z) const
  {
    ext_x = xmax-xmin;
    ext_y = ymax-ymin;
    ext_z = zmax-zmin;
  }
  void extents_half(float& ext_x, float& ext_y, float& ext_z) const
  {
    ext_x = (xmax-xmin)/2.0f;
    ext_y = (ymax-ymin)/2.0f;
    ext_z = (zmax-zmin)/2.0f;
  }
  float diagonal() const
  {
    return sqrt((xmax-xmin)*(xmax-xmin) + (ymax-ymin)*(ymax-ymin) + (zmax-zmin)*(zmax-zmin));
  }
  NEIGHBORING neighbors(BBox b2)
  {
	  MFloatPoint p0(xmin,ymin,zmin);
	  MFloatPoint p1(xmin+1,ymin,zmin);
	  MFloatPoint p2(xmin,ymin+1,zmin);
	  MFloatPoint p3(xmin,ymin,zmin+1);
	  MFloatPoint p4(xmin+1,ymin+1,zmin);
	  MFloatPoint p5(xmin+1,ymin,zmin+1);
	  MFloatPoint p6(xmin+1,ymin,zmin+1);
	  MFloatPoint p7(xmin+1,ymin+1,zmin+1);

	  MFloatPoint p00(b2.xmin,b2.ymin,b2.zmin);
	  MFloatPoint p11(b2.xmin+1,b2.ymin,b2.zmin);
	  MFloatPoint p22(b2.xmin,b2.ymin+1,b2.zmin);
	  MFloatPoint p33(b2.xmin,b2.ymin,b2.zmin+1);
	  MFloatPoint p44(b2.xmin+1,b2.ymin+1,b2.zmin);
	  MFloatPoint p55(b2.xmin+1,b2.ymin,b2.zmin+1);
	  MFloatPoint p66(b2.xmin+1,b2.ymin,b2.zmin+1);
	  MFloatPoint p77(b2.xmin+1,b2.ymin+1,b2.zmin+1);

	  int c = 0;
	  if(p0==p00 || p0==p11 || p0==p22 || p0==p33 || p0==p44 || p0==p55 || p0==p66 || p0==p77)
		  c++;
	  if(p1==p00 || p1==p11 || p1==p22 || p1==p33 || p1==p44 || p1==p55 || p1==p66 || p1==p77)
		  c++;
	  if(p2==p00 || p2==p11 || p2==p22 || p2==p33 || p2==p44 || p2==p55 || p2==p66 || p2==p77)
		  c++;
	  if(p3==p00 || p3==p11 || p3==p22 || p3==p33 || p3==p44 || p3==p55 || p3==p66 || p3==p77)
		  c++;
	  if(p4==p00 || p4==p11 || p4==p22 || p4==p33 || p4==p44 || p4==p55 || p4==p66 || p4==p77)
		  c++;
	  if(p5==p00 || p5==p11 || p5==p22 || p5==p33 || p5==p44 || p5==p55 || p5==p66 || p5==p77)
		  c++;
	  if(p6==p00 || p6==p11 || p6==p22 || p6==p33 || p6==p44 || p6==p55 || p6==p66 || p6==p77)
		  c++;
	  if(p7==p00 || p7==p11 || p7==p22 || p7==p33 || p7==p44 || p7==p55 || p7==p66 || p7==p77)
		  c++;
	  if(c==2)
		  return edge;
	  if(c==4)
		  return face;
	  if(c==1)
		  return vertex;
  }

  float xmin;
  float xmax;
  float ymin; 
  float ymax;
  float zmin;
  float zmax;
};

#endif